package com.leetcode;

import java.util.Scanner;
/**
 * The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...

1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.
Given an integer n, generate the nth sequence.

Note: The sequence of integers will be represented as a string.
 * @author Adiy
 *
 */
public class CountAndSay {

	public static void main(String[] args) {

		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		String result=countAndSay(n);
		System.out.println(result);
	}

	public static String countAndSay(int n) {
		String str="";
		String result="";
		if (n == 1)
			return "1";

		else{
			str=countAndSay(n-1);
			String[] arr=str.split("");
			
			int[] arrnum=new int[arr.length-1];
			for(int i=1;i<arr.length;i++){
				arrnum[i-1]=Integer.parseInt(arr[i]);
			}
			int count=0;
			for(int i=0;i<arrnum.length;){
				int j=i+1;count=1;
				while(j<arrnum.length&&arrnum[i]==arrnum[j]){
					count++;
					j++;
				}
				result+=""+count+arrnum[i];
				i+=count;
			}
		}
			
		return result;
	}
	
}
